#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <second.h>
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QFileInfo>
#include <iostream>
#include <QTextStream>
#include <QDebug>
#include <QDir>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowTitle("词频统计");
    QString curpath;
    curpath = QDir::currentPath();
    qDebug() << curpath;

    QString filename = "E:\\QT\\Project\\work\\punctuation.txt";
    readStopWord(filename);


    for(int i = 0; i < this->stopWord.size(); i++)
        {
            qDebug() << this->stopWord.at(i);
        }

    connect(ui->btn_select,&QPushButton::clicked,[=](){
    //选择文件路径
       path = QFileDialog::getOpenFileName(this,"打开文件","C:\\");

        if(path.isEmpty())
        {//选择的路径为空，提出警告
            QMessageBox::warning(this,"警告","打开失败");
        }else{

        //创建文件对象
        QFile file(path);

        //获取文件后缀名
        QString houzhui;
        QFileInfo info(file);
        houzhui = info.suffix();

        //获取文件内容
        QByteArray arr;
        file.open(QFileDevice::ReadOnly);
        arr = file.readAll();
        file.close();

        //判断所选文件是否是200M以内的英文txt文件
        if(Istxt(houzhui) && IsSize(file.size()) && IsEnglish(arr)){
            ui->lineEdit->setText(path);






        }
        else{

        }
        }

    });

    connect(ui->btn_OK,&QPushButton::clicked,[=](){
        //判断文件选择框是否为空
        if(ui->lineEdit->text().isEmpty())
        {//选择的路径为空，提出警告
            QMessageBox::warning(this,"警告","请选择文件路径");
        }
        else{

            //获取用户选择的算法选项
             index = ui->comboBox->currentIndex();

            Second *s = new Second ;

            connect(this,SIGNAL(SendData(QString,int)),s,SLOT(RecieveData(QString,int)));
            emit SendData(path,index);



            //显示子窗口
            s->show();
        }


    });

}

MainWindow::~MainWindow()
{
    delete ui;
}

//判断文件内容是否含有非法字符
bool MainWindow :: IsEnglish(QByteArray arr)
{
    QByteArray ba = arr;
    const char *ch = ba.data();
    while (*ch) {
         qDebug() << this->stopWord.indexOf(ch);
        if((*ch >= 'A' && *ch <= 'Z')||(*ch >= 'a' && *ch <= 'z') ||(*ch >= '0' && *ch <= '9' ) || isspace(*ch) || (!this->stopWord.indexOf(ch) == -1)   ){

        }else {
            QMessageBox::warning(this,"警告","请选择英文txt文件");
            return false;
        }

        ch++;
    }

    return true;

}

//判断文件后缀是否是txt
bool MainWindow :: Istxt(QString houzhui){
    if(houzhui == "txt"){
        return true;
    }
    else{
        QMessageBox::warning(this,"警告","请选择txt文件");
        return false;
    }

}

//判断文件大小是否在200M以内
bool MainWindow::IsSize(int size){
    if(size <= 209715200){
        return true;
    }else{
        QMessageBox::warning(this,"警告","请选择200M以内大小的文件");
        return false;
    }
}
void MainWindow::readStopWord(QString filename){
    QFile file(filename);
    file.open(QIODevice::ReadOnly);
    QString line;
    QTextStream in(&file);  //用文件构造流
    line = in.readLine();//读取一行放到字符串里
    this->stopWord.append(line);
    while(!line.isNull())//字符串有内容
    {
        line=in.readLine();//循环读取下行
        this->stopWord.append(line);
    }
}
